package at.ltasat.solve;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import at.ltasat.Configuration;

public class SolverConfiguration extends Configuration {

	private File classifierFile;
	private int max;
	private final List<String> args;
	private boolean isVerboseStatus, isVerboseConfig;

	{
		classifierFile = new File("classifier");
		max = Runtime.getRuntime().availableProcessors();
		args = new LinkedList<String>();
		isVerboseStatus = false;
		isVerboseConfig = false;
	}

	public static SolverConfiguration get(String[] args) {
		SolverConfiguration config = new SolverConfiguration();
		for (String arg : args) {
			if (arg.contains("=")) {
				String name = arg.substring(0, arg.indexOf('='));
				String value = arg.substring(arg.indexOf('=') + 1);
				if (name.equals("-v") || name.equals("--verbose")) {
					for (String vv : value.split(",")) {
						if (vv.equals("config")) {
							config.setVerboseConfig(true);
						} else if (vv.equals("status")) {
							config.setVerboseStatus(true);
						} else {
							throw new IllegalArgumentException("Unknown verbose mode: " + vv);
						}
					}
				} else if (name.equals("-m") || name.equals("--max")) {
					config.setMax(Integer.valueOf(value));
				} else if (name.equals("-a") || name.equals("--arg")) {
					config.addArg(value);
				} else if (name.equals("-c") || name.equals("--classifier")) {
					config.setClassifierFile(new File(value));
				} else {
					throw new IllegalArgumentException("Unknown property: " + name);
				}

			} else {
				config.setIn(new File(arg));
				break;
			}
		}
		return config;
	}

	public File getClassifierFile() {
		return classifierFile;
	}

	public int getMax() {
		return max;
	}

	public String[] getArgs() {
		return args.toArray(new String[args.size()]);
	}

	public boolean isVerboseStatus() {
		return isVerboseStatus;
	}

	public boolean isVerboseConfig() {
		return isVerboseConfig;
	}

	public void setClassifierFile(File file) {
		assert file != null;
		this.classifierFile = file;
	}

	public void setMax(int max) {
		if (max <= 0 || max > this.max) {
			throw new IllegalArgumentException("Max must be greater than zero and smaller or equal to number of processors");
		}
		this.max = max;
	}

	public void addArg(String arg) {
		args.add(arg);
	}

	public void setVerboseStatus(boolean verbose) {
		this.isVerboseStatus = verbose;
	}

	public void setVerboseConfig(boolean verbose) {
		this.isVerboseConfig = verbose;
	}

	@Override
	public String toString() {
		StringBuilder string = new StringBuilder();
		string.append(String.format("--verbose=%s,%s ", isVerboseConfig ? "config" : "", isVerboseStatus ? "status" : ""));
		string.append(String.format("--max=%d ", max));
		for (String arg : args) {
			string.append(String.format("--arg=\"%s\" ", arg));
		}
		string.append(String.format("%s ", classifierFile.getAbsolutePath()));
		if (isInFile()) {
			string.append(getInFile().getAbsolutePath());
		}
		return string.toString().trim();
	}

}
